<template>
  <view>
  <div class="app-container" v-if="!opendetail">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入店铺名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营区域" prop="operatingArea">
        <el-input
          v-model="queryParams.operatingArea"
          placeholder="请输入经营区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:merchant:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:merchant:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:merchant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:merchant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />-->
      <el-table-column label="店铺名" align="center" prop="name" />
      <el-table-column label="经营区域" align="center" prop="operatingArea" />
      <el-table-column label="经营人" align="center" prop="director" />
      <el-table-column label="注册时间" align="center" prop="regTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.regTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="商家介绍" align="center" prop="description" />
      <el-table-column label="审核状态" align="center" prop="auditStatusId" >
        <template #default="scope">
          <el-tag v-if="scope.row.auditStatusId === 0" type="info">待审核</el-tag>
          <el-tag v-if="scope.row.auditStatusId === 1" type="success">已审核</el-tag>
          <el-tag v-if="scope.row.auditStatusId === 2" type="danger">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:merchant:edit']">修改</el-button>-->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:merchant:remove']">删除</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDetail(scope.row)" v-hasPermi="['system:merchant:edit']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改商户对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="merchantRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名" prop="name">
          <el-input v-model="form.name" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="经营区域" prop="operatingArea">
          <el-input v-model="form.operatingArea" placeholder="请输入经营区域" />
        </el-form-item>
        <el-form-item label="注册时间" prop="regTime">
          <el-date-picker clearable
            v-model="form.regTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始营业时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始营业时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停止营业时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择停止营业时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="商家介绍" prop="description">
          <el-input v-model="form.description" placeholder="请输入商家介绍" />
        </el-form-item>
        <el-form-item label="商家评分" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入商家评分" />
        </el-form-item>
        <el-form-item label="删除状态" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入删除状态" />
        </el-form-item>
        <el-form-item label="审核状态(0待审核、1已审核、2已拒绝)" prop="auditStatusId">
          <el-input v-model="form.auditStatusId" placeholder="请输入审核状态(0待审核、1已审核、2已拒绝)" />
        </el-form-item>
        <el-form-item label="当前金额" prop="currentBalance">
          <el-input v-model="form.currentBalance" placeholder="请输入当前金额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <view class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </view>
      </template>
    </el-dialog>
  </div>
  <div v-if="opendetail" class="merchant-detail-container">
    <div class="detail-header">
      <h1 class="detail-title">商户详细信息</h1>
      <el-button type="primary" icon="Back" @click="opendetail=false" class="back-btn">返回列表</el-button>
    </div>
    
    <div class="detail-content">
      <!-- 基本信息卡片 -->
      <el-card class="info-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <el-icon><User /></el-icon>
            <span>基本信息</span>
          </div>
        </template>
        <div class="info-grid">
          <div class="info-item">
            <label class="info-label">商户名称：</label>
            <span class="info-value">{{ form1.name }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">营业执照号码：</label>
            <span class="info-value">{{ form1.businessLicense }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">负责人：</label>
            <span class="info-value">{{ form1.director }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">负责人手机号：</label>
            <span class="info-value">{{ form1.mobile }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">身份证号码：</label>
            <span class="info-value">{{ form1.idCard }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">经营区域：</label>
            <span class="info-value">{{ form1.operatingArea }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">注册时间：</label>
            <span class="info-value">{{ form1.regTime }}</span>
          </div>
          <div class="info-item">
            <label class="info-label">审核状态：</label>
            <span class="info-value">
              <el-tag v-if="form1.auditStatusId===1" type="success" size="large">审核通过</el-tag>
              <el-tag v-if="form1.auditStatusId===0" type="warning" size="large">待审核</el-tag>
              <el-tag v-if="form1.auditStatusId===2" type="danger" size="large">已拒绝</el-tag>
            </span>
          </div>
        </div>
      </el-card>

      <!-- 地址和介绍卡片 -->
      <el-card class="info-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <el-icon><Location /></el-icon>
            <span>地址与介绍</span>
          </div>
        </template>
        <div class="info-section">
          <div class="info-item full-width">
            <label class="info-label">详细地址：</label>
            <span class="info-value">{{ form1.address }}</span>
          </div>
          <div class="info-item full-width">
            <label class="info-label">商家介绍：</label>
            <div class="description-content">{{ form1.description }}</div>
          </div>
        </div>
      </el-card>

      <!-- 商户照片卡片 -->
      <el-card class="info-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <el-icon><Picture /></el-icon>
            <span>商户照片</span>
          </div>
        </template>
        <div class="photo-section">
          <div class="photo-item">
            <label class="photo-label">门面照片：</label>
            <div class="photo-container">
              <el-image 
                src="https://via.placeholder.com/200x150?text=门面照片" 
                fit="cover"
                class="photo-image"
                :preview-src-list="['https://via.placeholder.com/800x600?text=门面照片']"
              />
            </div>
          </div>
          <div class="photo-item">
            <label class="photo-label">营业执照：</label>
            <div class="photo-container">
              <el-image 
                src="https://via.placeholder.com/200x150?text=营业执照" 
                fit="cover"
                class="photo-image"
                :preview-src-list="['https://via.placeholder.com/800x600?text=营业执照']"
              />
            </div>
          </div>
          <div class="photo-item">
            <label class="photo-label">身份证照片：</label>
            <div class="photo-container">
              <el-image 
                src="https://via.placeholder.com/200x150?text=身份证正面" 
                fit="cover"
                class="photo-image"
                :preview-src-list="['https://via.placeholder.com/800x600?text=身份证正面']"
              />
              <el-image 
                src="https://via.placeholder.com/200x150?text=身份证反面" 
                fit="cover"
                class="photo-image"
                :preview-src-list="['https://via.placeholder.com/800x600?text=身份证反面']"
              />
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons" v-if="form1.auditStatusId===0">
      <el-button type="success" size="large" icon="Check" @click="pass" class="action-btn">
        审核通过
      </el-button>
      <el-button type="danger" size="large" icon="Close" @click="out" class="action-btn">
        审核不通过
      </el-button>
    </div>
  </div>
  </view>
</template>

<script setup name="Merchant">
import { listMerchant, getMerchant, delMerchant, addMerchant, updateMerchant } from "../../../api/system/merchant.js"
import {listVerification} from "../../../api/system/verification.js"
import { User, Location, Picture, Back, Check, Close } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

const merchantList = ref([])
const open = ref(false)
const form1=ref({})
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const opendetail = ref(false)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    operatingArea: null,
    regTime: null,
    startTime: null,
    endTime: null,
    address: null,
    longitude: null,
    latitude: null,
    description: null,
    rate: null,
    status: null,
    deleted: null,
    auditStatusId: null,
    currentBalance: null
  },
  rules: {
    name: [
      { required: true, message: "店铺名不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商户列表 */
function getList() {
  loading.value = true
  listMerchant(queryParams.value).then(response => {
    let a=[]
    let b=[]
    total.value = response.total
    loading.value = false
    a=response.rows
    console.log(a)
    listVerification().then(response => {
      b=response.rows
      for (let i=0;i<a.length;i++){
        for (let j=0;j<b.length;j++){
          if(a[i].id===b[j].mid){
            a[i].director=b[j].director
            a[i].mobile=b[j].mobile
            a[i].idCard=b[j].idCard
            a[i].businessLicense=b[j].businessLicenseId
          }
        }
      }
      merchantList.value= a
    })
  })
}
//展示详情
function handleDetail(row) {
  getMerchant(row.id).then(response => {
    opendetail.value=true
    form1.value = row
    console.log(form1.value)
  })
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    operatingArea: null,
    regTime: null,
    startTime: null,
    endTime: null,
    address: null,
    longitude: null,
    latitude: null,
    description: null,
    rate: null,
    status: null,
    deleted: null,
    auditStatusId: null,
    currentBalance: null,
    businessLicenseId: null,
    director: null,
    idCard: null,
    mobile: null,
  }
  proxy.resetForm("merchantRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商户"
}
//审核通过
function pass(){
  form1.value.auditStatusId = 1
  updateMerchant(form1.value).then(response=>{
    console.log(form1.value)
    opendetail.value = false
  })
}
//审核不通过
function out(){
  form1.value.auditStatusId = 2
  updateMerchant(form1.value).then(response=>{
    opendetail.value = false
  })
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMerchant(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商户"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除商户编号为"' + _ids + '"的数据项？').then(function() {
    return delMerchant(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/merchant/export', {
    ...queryParams.value
  }, `merchant_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<style scoped lang="scss">
.merchant-detail-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.detail-title {
  color: white;
  font-size: 28px;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.back-btn {
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  font-weight: 500;
  
  &:hover {
    background-color: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
  }
}

.detail-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.info-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  }
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  
  .el-icon {
    font-size: 20px;
    color: #409eff;
  }
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.info-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #409eff;
  transition: all 0.3s ease;
  
  &:hover {
    background-color: #e3f2fd;
    transform: translateX(4px);
  }
  
  &.full-width {
    grid-column: 1 / -1;
  }
}

.info-label {
  font-weight: 600;
  color: #2c3e50;
  min-width: 120px;
  margin-right: 16px;
  font-size: 14px;
}

.info-value {
  color: #34495e;
  font-size: 14px;
  flex: 1;
  
  .el-tag {
    font-weight: 500;
  }
}

.description-content {
  background-color: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #67c23a;
  color: #34495e;
  line-height: 1.6;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.photo-section {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.photo-item {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.photo-label {
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
  margin-bottom: 8px;
}

.photo-container {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.photo-image {
  width: 200px;
  height: 150px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  
  &:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  }
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 40px;
  padding: 30px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.action-btn {
  min-width: 160px;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .detail-header {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .detail-title {
    font-size: 24px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .photo-container {
    justify-content: center;
  }
  
  .photo-image {
    width: 100%;
    max-width: 300px;
  }
  
  .action-buttons {
    flex-direction: column;
    align-items: center;
  }
  
  .action-btn {
    width: 100%;
    max-width: 300px;
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.info-card {
  animation: fadeInUp 0.6s ease-out;
  
  &:nth-child(1) { animation-delay: 0.1s; }
  &:nth-child(2) { animation-delay: 0.2s; }
  &:nth-child(3) { animation-delay: 0.3s; }
}
</style>
